/*global analogue*/
/*jslint indent: 4*/

/*Analogue.MSIE 1.11.12, Copyright (c) 2011 Analogue Web Design LLC, all rights reserved.*/
analogue.msie = (function (document, window, $) {
    'use strict';

    var activators = {
            onactive: {
                on: 'mousedown',
                off: 'mouseup'
            },
            onfocus: {
                on: 'focus',
                off: 'blur'
            },
            onhover: {
                on: 'mouseover',
                off: 'mouseout'
            },
            onunknown: {
                on: 'focus',
                off: 'blur'
            }
        },

        implement = function (selector, value, activator) {
            $(document).addDelegate(selector, activator.on, function () {
                $(this).addClass(value);
            }).addDelegate(selector, activator.off, function () {
                $(this).removeClass(value);
            });
        },

        msie = !document.documentMode ? typeof window.XMLHttpRequest !== 'object' ? 6 : 7 : document.documentMode,

        pseudoExp = (function () {
            /*jslint regexp: true*/
            if (msie === 6) {
                return (/(^|\s)((([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active))|((a|input|textarea)([#.][^ ]+)?:unknown)/i);
            }
            if (msie === 7) {
                return (/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(active|focus)/i);
            }
            return (/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(active)/i);
        }());

    function parse(sheet) {
        /*jslint regexp: true*/
        var css,
            i,
            ii,
            match,
            pseudo,
            rule,
            rules,
            selected,
            selector,
            value;
        if (sheet.imports) {
            for (i = 0, ii = sheet.imports.length; i < ii; i += 1) {
                parse(sheet.imports[i]);
            }
        }
        rules = sheet.rules;
        for (i = 0, ii = rules.length; i < ii; i += 1) {
            rule = rules[i];
            selector = rule.selectorText;
            css = rule.style.cssText;
            if ((match = /opacity:\s*([\d]*[\.\d+]*)/i.exec(css)) !== null) {
                if (msie > 7) {
                    sheet.addRule(selector, '-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=' + parseFloat(match[1] * 100) + ')";');
                }
                sheet.addRule(selector, 'zoom: 1; filter: alpha(opacity=' + parseFloat(match[1] * 100) + ');');
            }
            if (pseudoExp.test(selector)) {
                pseudo = selector.replace(/[^:]+:([\w]+)[^\n]*/i, 'on$1');
                selected = selector.replace(/(\.([a-z0-9_\-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
                value = /\.([a-z0-9_\-]*on(hover|active|focus|unknown))/i.exec(selected)[1];
                sheet.addRule(selected, css);
                implement(selector.replace(/:(hover|active|focus|unknown)[^\n]*$/, ''), value, activators[pseudo]);
            }
        }
    }

    if (msie === 6) {
        document.execCommand('BackgroundImageCache', false, true);
        $('<abbr/>');
        $('<meta/>').set({
            'http-equiv': 'imagetoolbar',
            content: 'no'
        }).insertBefore($('script')[0]);
        window.XMLHttpRequest = function () {
            var i,
                ii,
                msxml = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'],
                xhr = null;
            for (i = 0, ii = msxml.length; i < ii; i += 1) {
                try {
                    xhr = new window.ActiveXObject(msxml[i]);
                    break;
                } catch (e) {
                    xhr = null;
                }
            }
            if (xhr !== null) {
                window.XMLHttpRequest = function () {
                    return new window.ActiveXObject(msxml[i]);
                };
            }
            return xhr;
        };
    }

    $(document).ready(function () {
        var i,
            ii,
            sheets = document.styleSheets;
        for (i = 0, ii = sheets.length; i < ii; i += 1) {
            parse(sheets[i]);
        }
    });

    return {
        version: msie
    };
}(this.document, this, analogue));